package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domian.Orders;
import com.itheima.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private IOrdersService iOrdersService;

//    查询全部订单--未分页
//    @RequestMapping("/findAll.do")
//    public ModelAndView findAll() throws Exception {
//        System.out.println("orders findall");
//        ModelAndView mv = new ModelAndView();
//        List<Orders> ordersLists = iOrdersService.findAll();
//        mv.addObject("ordersList",ordersLists);
//        mv.setViewName("orders-list");
//        return mv;
//
//    }


    //    查询全部订单--分页
    @RequestMapping("/findAll.do")
    //@Secured("ROLE_ADMIN")
    public ModelAndView findAll(
            @RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
            @RequestParam(name = "size", required = true, defaultValue = "3") Integer size)
            throws Exception {
        System.out.println("orders findall");
        ModelAndView mv = new ModelAndView();
        List<Orders> ordersLists = iOrdersService.findAll(page,size);
//        pageInfo就是一个分页的bean
        PageInfo pageInfo = new PageInfo(ordersLists);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("orders-page-list");
        return mv;
    }

    @RequestMapping("/findById.do")
    public ModelAndView findById(
            @RequestParam(name = "id", required = true ) String  id
    ) throws Exception {
        ModelAndView mv = new ModelAndView();
        Orders orders = iOrdersService.findById(id);
        mv.addObject("orders",orders);
        mv.setViewName("orders-show");
        return mv;
    }
}
